package com.ruoyi.kjcg.sbzn.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.kjcg.sbzn.domain.Sbzn;
import com.ruoyi.kjcg.sbzn.domain.SbznFj;
import com.ruoyi.kjcg.sbzn.mapper.SbznMapper;
import com.ruoyi.kjcg.sbzn.service.ISbznService;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 申报指南Service业务层处理
 *
 * @author huacai
 * @date 2024-11-07
 */
@Service
public class SbznServiceImpl implements ISbznService {
    @Resource
    private SbznMapper sbznMapper;

    @Resource
    private SqlSessionFactory sqlSessionFactory;

    /**
     * 查询申报指南
     *
     * @param sbznId 申报指南主键
     * @return 申报指南
     */
    @Override
    public Sbzn selectSbznBySbznId(String sbznId) {
        return sbznMapper.selectSbznBySbznId(sbznId);
    }

    /**
     * 查询申报指南列表
     *
     * @param sbzn 申报指南
     * @return 申报指南
     */
    @Override
    public List<Sbzn> selectSbznList(Sbzn sbzn) {
        return sbznMapper.selectSbznList(sbzn);
    }

    /**
     * 新增申报指南
     *
     * @param sbzn 申报指南
     * @return 结果
     */
    @Transactional
    @Override
    public int insertSbzn(Sbzn sbzn) {
        sbzn.setCreateTime(DateUtils.getNowDate());
        int rows = sbznMapper.insertSbzn(sbzn);
        insertSbznFj(sbzn);
        return rows;
    }

    /**
     * 批量新增申报指南
     *
     * @param sbzns 申报指南List
     * @return 结果
     */
    @Override
    public int batchInsertSbzn(List<Sbzn> sbzns) {
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        int count = 0;
        if (!CollectionUtils.isEmpty(sbzns)) {
            try {
                for (int i = 0; i < sbzns.size(); i++) {
                    int row = sbznMapper.insertSbzn(sbzns.get(i));
                    // 防止内存溢出，每100次提交一次,并清除缓存
                    boolean bool = (i > 0 && i % 100 == 0) || i == sbzns.size() - 1;
                    if (bool) {
                        sqlSession.commit();
                        sqlSession.clearCache();
                    }
                    count = i + 1;
                }
            } catch (Exception e) {
                e.printStackTrace();
                // 没有提交的数据可以回滚
                sqlSession.rollback();
            } finally {
                sqlSession.close();
                return count;
            }
        }
        return count;
    }

    /**
     * 修改申报指南
     *
     * @param sbzn 申报指南
     * @return 结果
     */
    @Transactional
    @Override
    public int updateSbzn(Sbzn sbzn) {
        sbzn.setUpdateTime(DateUtils.getNowDate());
//        sbznMapper.deleteSbznFjBySbznId(sbzn.getSbznId());
        insertSbznFj(sbzn);
        return sbznMapper.updateSbzn(sbzn);
    }

    /**
     * 批量删除申报指南
     *
     * @param sbznIds 需要删除的申报指南主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSbznBySbznIds(String[] sbznIds) {
        sbznMapper.deleteSbznFjBySbznIds(sbznIds);
        return sbznMapper.deleteSbznBySbznIds(sbznIds);
    }

    /**
     * 删除申报指南信息
     *
     * @param sbznId 申报指南主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSbznBySbznId(String sbznId) {
        sbznMapper.deleteSbznFjBySbznId(sbznId);
        return sbznMapper.deleteSbznBySbznId(sbznId);
    }

    /**
     * 新增附件
     *
     * @param sbznFj
     * @return
     */
    @Override
    public int insertSbznFjBySbznId(SbznFj sbznFj) {
        return sbznMapper.insertSbznFjBySbznId(sbznFj);
    }

    /**
     * 根据ID查询附件列表
     *
     * @param sbznFj
     * @return
     */
    @Override
    public List<SbznFj> selectSbznFjList(SbznFj sbznFj) {
        return sbznMapper.selectSbznFjList(sbznFj);
    }

    /**
     * 批量删除申报指南附件
     *
     * @param fjIds 需要删除的申报指南附件主键
     * @return 结果
     */
    @Override
    public int deleteSbznFjByFjIds(Integer[] fjIds) {
        return sbznMapper.deleteSbznFjByFjIds(fjIds);
    }

    /**
     * 新增申报指南附件信息
     *
     * @param sbzn 申报指南对象
     */
    public void insertSbznFj(Sbzn sbzn) {
        List<SbznFj> sbznFjList = sbzn.getSbznFjList();
        String sbznId = sbzn.getSbznId();
        if (StringUtils.isNotNull(sbznFjList)) {
            List<SbznFj> list = new ArrayList<SbznFj>();
            for (SbznFj sbznFj : sbznFjList) {
                sbznFj.setSbznId(sbznId);
                list.add(sbznFj);
            }
//            if (list.size() > 0) {
//                sbznMapper.batchSbznFj(list);
//            }
        }
    }
}
